package top.hkyzf.study.j2se.juc;


import lombok.AllArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.junit.Test;

import java.util.concurrent.atomic.AtomicReference;

/**
 * 原子引用
 * @author 朱峰
 * @date 2020-12-24 10:59
 */
public class AtomicReferenceDemo {
    @Test
    public void testAtomicReference () {
        User user1 = new User("z3", 20);
        User user2 = new User("li4", 25);

        AtomicReference<User> atomicReference = new AtomicReference<>();
        atomicReference.set(user1);
        System.out.println(atomicReference.compareAndSet(user1, user2) + "\t" + atomicReference.get().toString());
        System.out.println(atomicReference.compareAndSet(user1, user2) + "\t" + atomicReference.get().toString());
    }
}

@Setter
@AllArgsConstructor
@ToString
class User {
    String name;
    int age;
}
